package com.liukaixin.product.navi;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liukaixin on 16/10/12.
 */

public class ParkCarFragment extends LazyLoadFragment
        implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    private GaragesAdapter garagesAdapter;

    private Handler handler = new Handler();

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static ParkCarFragment newInstance(int sectionNumber) {
        ParkCarFragment fragment = new ParkCarFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public ParkCarFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_park_car, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        garagesAdapter = new GaragesAdapter(getActivity());
        // Set up the competitions view
        EasyRecyclerView recyclerView = (EasyRecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapterWithProgress(garagesAdapter);
        recyclerView.setRefreshListener(this);

        garagesAdapter.setMore(R.layout.view_more, this);
        garagesAdapter.setNoMore(R.layout.view_no_more);

        garagesAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "请稍后,加载车库地图中……", Toast.LENGTH_SHORT).show();
                Intent toMap = new Intent(getActivity(), MapActivity.class);
                startActivity(toMap);
            }
        });

        garagesAdapter.setError(R.layout.view_error)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        garagesAdapter.resumeMore();
                    }
                });
    }

    @Override
    public void initData() {
        onRefresh();
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Garage garage = new Garage("RLL超酷停车场", 1.5f, 5);
                List<Garage> garages = new ArrayList<>();
                garages.add(garage);
                garagesAdapter.addAll(garages);
                garagesAdapter.stopMore();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {

    }
}
